package restaurant.iterators;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import restaurant.MenuItem;

/**
 * An iterator to view the menu by price.
 * 
 * @author Odessa
 */

public class ByPriceIterator implements Iterator {

	/** The price to view the menu by. */
	private double price;

	/** The iterator through the menu. */
	private java.util.Iterator<MenuItem> menu_iterator;

	/** The next item in the menu. */
	private MenuItem nextItem;

	/**
	 * Instantiates a new by price iterator.
	 * 
	 * @param menu
	 *            the menu of items.
	 * @param price
	 *            the price by which to view the menu.
	 */
	public ByPriceIterator(ArrayList<MenuItem> menu, double price) {
		this.price = price;
		menu_iterator = menu.iterator();
	}

	/**
	 * Determines if there is another item on the menu with a lower price than
	 * the one specified.
	 * 
	 * @return {@code true} if there are more items on the menu which have a
	 *         lower price than the one specified.
	 * @see restaurant.iterators.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext() {
		boolean matchFound = false;

		while (menu_iterator.hasNext()) {
			MenuItem temp = (MenuItem) menu_iterator.next();

			if (temp.getCurrentPrice() <= price) {
				matchFound = true;
				nextItem = temp;
				break;
			}
		}

		if (matchFound == true) {
		} else
			nextItem = null;

		return matchFound;
	}

	/**
	 * Returns the next item on the menu with a lower price than the one
	 * specified.
	 * 
	 * @return the next item on the menu with a lower price than the one
	 *         specified.
	 * @throws NoSuchElementException
	 *             if the menu has no more items with a lower price.
	 * @see restaurant.iterators.Iterator#next()
	 */
	@Override
	public Object next() throws NoSuchElementException {
		if (nextItem == null) {
			throw new NoSuchElementException(
					"There are no more items on the menu with a lower price.");
		} else
			return nextItem;
	}

}
